package com.bocai.yuweiwangluo.presenter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;

import com.bocai.yuweiwangluo.adapter.GameListAdapter;
import com.bocai.yuweiwangluo.base.BaseView;
import com.bocai.yuweiwangluo.bean.GameListBean;
import com.bocai.yuweiwangluo.net.MyOkHttpClient;
import com.bocai.yuweiwangluo.utils.UrlData;
import com.bocai.yuweiwangluo.viewModel.IGameListView;
import com.google.gson.Gson;


/**
 * Created by admin on 2015/12/16.
 */
public class GameListPresenter
{
    private GameListBean mGameListBean=new GameListBean();
    private static final int RESPONSE_SUCCEED = 1;
    private static final int RESPONSE_FAILED = 2;
    private IGameListView gameListView;
    private Context context;
    private BaseView baseView;
    private GameListAdapter mGameListAdapter;
    public GameListPresenter(Context context,IGameListView gameListView,BaseView baseView)
    {
        this.context = context;
        this.gameListView = gameListView;
        this.baseView=baseView;
    }

    /**
     * 比赛列表
     */
    public void getGameList(String page,String type_id,String city_id)
    {
        baseView.showLoading();
        MyOkHttpClient myOkHttpClient = new MyOkHttpClient(context,baseView);
        myOkHttpClient.getGameList(UrlData.BASEURL + UrlData.GAMELIST,page,type_id, city_id, handler);
    }
    public GameListBean getData(){

        return mGameListBean;
    }
    private Handler handler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            super.handleMessage(msg);

            String result = (String) msg.obj;
            Gson gson = new Gson();
            gameListView.onLoad();
            try
            {
                switch (msg.what)
                {
                    case RESPONSE_SUCCEED:
                        baseView.hideLoading();
                        Log.e("loginresult", result);
                        mGameListBean= gson.fromJson(result, GameListBean.class);
                        if(mGameListBean.getData().size()==0&gameListView.getmyPage()==1){
                            gameListView.getNoContent().setVisibility(View.VISIBLE);
                            gameListView.getListView().setVisibility(View.GONE);
                        }else{
                            gameListView.getNoContent().setVisibility(View.GONE);
                            gameListView.getListView().setVisibility(View.VISIBLE);
                            if(gameListView.getmyPage()==1){
                                mGameListAdapter=new GameListAdapter(context,mGameListBean);
                                gameListView.getListView().setAdapter(mGameListAdapter);
                            }else{
                                mGameListAdapter.addData(mGameListBean);
                            }
                            if(mGameListBean.getTotal()<=(mGameListBean.getEach()*gameListView.getmyPage())){
                                gameListView.getListView().setPullLoadEnable(false);
                            }
                        }
                        break;
                    case RESPONSE_FAILED:
                        baseView.hideLoading();
                        baseView.showError(result);
                        gameListView.getllRefresh().setVisibility(View.VISIBLE);
                        break;
                }

            } catch (Exception e)
            {
            }
        }

    };
}